package validator

import (
	"fmt"
	"reflect"
	"strings"
)

type StringIn struct {
	array []string
	notIn bool
}

func NewStringInValidator(array []string, notIn bool) *StringIn {
	return &StringIn{
		array: array,
		notIn: notIn,
	}
}
func (in *StringIn) String() string {
	// var items string
	// for n, v := range in.array {
	// 	if n == 0 {
	// 		items = fmt.Sprintf("%s", v)
	// 	} else {
	// 		items = fmt.Sprintf("%s,%s", items, v)
	// 	}
	// }
	if in.notIn {
		return fmt.Sprintf("(%s)", strings.Join(in.array, ","))
	}
	return fmt.Sprintf("[%s]", strings.Join(in.array, ","))
}

func (in *StringIn) IsValidate(field string, data reflect.Value) error {
	var v = data.String()
	if in.notIn {
		for _, item := range in.array {
			if v == item {
				return fmt.Errorf("field [%s]'s must not be one of %v", field, in.array)
			}
		}
		return nil
	} else {
		for _, item := range in.array {
			if v == item {
				return nil
			}
		}
		return fmt.Errorf("field [%s]'s must be one of %v", field, in.array)
	}
}
